﻿package com.yy.ui.core {
    import com.yy.manager.UiManager;
    
    import flash.display.*;
    import flash.events.*;

	/**
	 *通用组件基类 
	 * @author YY
	 * 
	 */
    public class UIComponent extends UISprite implements ITipComponent {

        protected var mouseOverStatus:Boolean = false;
        protected var mouseDownStatus:Boolean = false;
        private var _skin:*;
        private var _background:DisplayObject;
        private var _backgroundChange:Boolean = false;
        private var _sizeChange:Boolean = false;
        private var _autoSize:Boolean = true;
        private var _w:Number = 0;
        private var _h:Number = 0;
        private var _toolTip:String = "";
        private var _toolTipData:Object;
        private var _toolTipType:String = "simple";
        private var _toolTipDirection:String = "down";

        public function UIComponent(){
            if (this.stage){
                this.initComponent();
            } else {
                addEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            };
            this.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
            this.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);
        }
        private function addedToStageHandler(_arg1:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE, this.addedToStageHandler);
            this.initComponent();
        }
        protected function initComponent():void{
            addEventListener(Event.RENDER, this.onRender);
            if (this.stage){
                this.stage.invalidate();
            };
        }
        private function onRender(e:Event):void{
            this.updateDisplayList();
        }
        protected function updateDisplayList():void{
            this.updateUI();
        }
        protected function onMouseOver(_arg1:MouseEvent):void{
            this.mouseOverStatus = true;
            this.mouseOver();
//            UiManager.getMouseManager().mouseOver(this);
            UiManager.getToolTipManager().mouseOver(this);
        }
        protected function onMouseOut(_arg1:MouseEvent):void{
            this.mouseOverStatus = false;
            this.mouseOut();
//            UiManager.getMouseManager().mouseOut(this);
            UiManager.getToolTipManager().mouseOut(this);
        }
        private function onMouseDown(_arg1:MouseEvent):void{
            this.mouseDownStatus = true;
            this.mouseDown();
        }
        private function onMouseUp(_arg1:MouseEvent):void{
            this.mouseDownStatus = false;
            this.mouseUp();
        }
        protected function mouseOver():void{
        }
        protected function mouseOut():void{
        }
        protected function mouseDown():void{
        }
        protected function mouseUp():void{
        }
        protected function get background():DisplayObject{
            return (this._background);
        }
        protected function set background(bg:DisplayObject):void{
            if (((this._background) && (this.contains(this._background)))){
                super.removeChild(this._background);
            };
            this._background = bg;
            this._backgroundChange = true;
            if (this._background){
                if ((this._background is MovieClip)){
                    (this._background as MovieClip).gotoAndStop(1);
                };
                super.addChildAt(this._background, 0);
            };
            this.updateUI();
        }
        public function get skin():*{
            return (this._skin);
        }
        public function set skin(sk:*):void{
            this._skin = sk;
            if ((sk is MovieClip)){
                this.background = (sk as MovieClip);
            } else {
                if ((sk is DisplayObject)){
                    this.background = (sk as DisplayObject);
                } else {
                    this.background = null;
                    return;
                };
            };
        }
        public function get autoSize():Boolean{
            return (this._autoSize);
        }
        public function set autoSize(_arg1:Boolean):void{
            this._autoSize = _arg1;
        }
        override public function get width():Number{
            return ((this.autoSize) ? super.width : this._w);
        }
        override public function set width(_arg1:Number):void{
            this._w = _arg1;
            this._sizeChange = true;
            if (this.stage){
                this.stage.invalidate();
            };
        }
        override public function get height():Number{
            return ((this.autoSize) ? super.height : this._h);
        }
        override public function set height(_arg1:Number):void{
            this._h = _arg1;
            this._sizeChange = true;
            if (this.stage){
                this.stage.invalidate();
            };
        }
        public function get toolTip():String{
            return (this._toolTip);
        }
        public function set toolTip(_arg1:String):void{
            this._toolTip = _arg1;
        }
        public function get toolTipData():Object{
            return (this._toolTipData);
        }
        public function set toolTipData(_arg1:Object):void{
            this._toolTipData = _arg1;
        }
        public function get toolTipType():String{
            return (this._toolTipType);
        }
        public function set toolTipType(_arg1:String):void{
            this._toolTipType = _arg1;
        }
        public function get toolTipDirection():String{
            return (this._toolTipDirection);
        }
        public function set toolTipDirection(_arg1:String):void{
            this._toolTipDirection = _arg1;
        }
        public function updateUI():void{
            if ((((this._backgroundChange == true)) && (!((this.background == null))))){
                this._backgroundChange = false;
                if (this.width == 0){
                    this.width = this.background.width;
                } else {
                    this.background.width = this.width;
                };
                if (this.height == 0){
                    this.height = this.background.height;
                } else {
                    this.background.height = this.height;
                };
            };
            if ((((this._sizeChange == true)) && (!((this.background == null))))){
                this._sizeChange = false;
                this.background.width = this.width;
                this.background.height = this.height;
            };
        }

    }
}